

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>nlp_architect.procedures.token_tagging &mdash; NLP Architect by Intel® AI Lab 0.5.2 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../_static/language_data.js"></script>
        <script type="text/javascript" src="../../../_static/install.js"></script>
        <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/nlp_arch_theme.css" type="text/css" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Mono" type="text/css" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:100,900" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html">
          

          
            
            <img src="../../../_static/logo.png" class="logo" alt="Logo"/>
          
          </a>

          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../../quick_start.html">Quick start</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../installation.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../publications.html">Publications</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../tutorials.html">Jupyter Tutorials</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../model_zoo.html">Model Zoo</a></li>
</ul>
<p class="caption"><span class="caption-text">NLP/NLU Models</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../tagging/sequence_tagging.html">Sequence Tagging</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../sentiment.html">Sentiment Analysis</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../bist_parser.html">Dependency Parsing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../intent.html">Intent Extraction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../lm.html">Language Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../information_extraction.html">Information Extraction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../transformers.html">Transformers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../archived/additional.html">Additional Models</a></li>
</ul>
<p class="caption"><span class="caption-text">Optimized Models</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../quantized_bert.html">Quantized BERT</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../transformers_distillation.html">Transformers Distillation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../sparse_gnmt.html">Sparse Neural Machine Translation</a></li>
</ul>
<p class="caption"><span class="caption-text">Solutions</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../absa_solution.html">Aspect Based Sentiment Analysis</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../term_set_expansion.html">Set Expansion</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../trend_analysis.html">Trend Analysis</a></li>
</ul>
<p class="caption"><span class="caption-text">For Developers</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../generated_api/nlp_architect_api_index.html">nlp_architect API</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../developer_guide.html">Developer Guide</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">NLP Architect by Intel® AI Lab</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>nlp_architect.procedures.token_tagging</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for nlp_architect.procedures.token_tagging</h1><div class="highlight"><pre>
<span></span><span class="c1"># ******************************************************************************</span>
<span class="c1"># Copyright 2017-2019 Intel Corporation</span>
<span class="c1">#</span>
<span class="c1"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1"># you may not use this file except in compliance with the License.</span>
<span class="c1"># You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1">#     http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1"># ******************************************************************************</span>
<span class="kn">import</span> <span class="nn">argparse</span>
<span class="kn">import</span> <span class="nn">io</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">from</span> <span class="nn">torch.utils.data</span> <span class="kn">import</span> <span class="n">DataLoader</span><span class="p">,</span> <span class="n">RandomSampler</span><span class="p">,</span> <span class="n">SequentialSampler</span>

<span class="kn">from</span> <span class="nn">nlp_architect.data.sequential_tagging</span> <span class="kn">import</span> <span class="n">TokenClsInputExample</span><span class="p">,</span> <span class="n">TokenClsProcessor</span>
<span class="kn">from</span> <span class="nn">nlp_architect.data.utils</span> <span class="kn">import</span> <span class="n">write_column_tagged_file</span>
<span class="kn">from</span> <span class="nn">nlp_architect.models.tagging</span> <span class="kn">import</span> <span class="n">NeuralTagger</span>
<span class="kn">from</span> <span class="nn">nlp_architect.nn.torch.modules.embedders</span> <span class="kn">import</span> <span class="n">IDCNN</span><span class="p">,</span> <span class="n">CNNLSTM</span>
<span class="kn">from</span> <span class="nn">nlp_architect.nn.torch</span> <span class="kn">import</span> <span class="n">setup_backend</span><span class="p">,</span> <span class="n">set_seed</span>
<span class="kn">from</span> <span class="nn">nlp_architect.nn.torch.distillation</span> <span class="kn">import</span> <span class="n">TeacherStudentDistill</span>
<span class="kn">from</span> <span class="nn">nlp_architect.procedures.procedure</span> <span class="kn">import</span> <span class="n">Procedure</span>
<span class="kn">from</span> <span class="nn">nlp_architect.procedures.registry</span> <span class="kn">import</span> <span class="n">register_train_cmd</span><span class="p">,</span> <span class="n">register_inference_cmd</span>
<span class="kn">from</span> <span class="nn">nlp_architect.utils.embedding</span> <span class="kn">import</span> <span class="n">get_embedding_matrix</span><span class="p">,</span> <span class="n">load_embedding_file</span>
<span class="kn">from</span> <span class="nn">nlp_architect.utils.io</span> <span class="kn">import</span> <span class="n">prepare_output_path</span>
<span class="kn">from</span> <span class="nn">nlp_architect.utils.text</span> <span class="kn">import</span> <span class="n">SpacyInstance</span>
<span class="kn">from</span> <span class="nn">nlp_architect.nn.torch.data.dataset</span> <span class="kn">import</span> <span class="n">ParallelDataset</span>
<span class="kn">from</span> <span class="nn">nlp_architect.models.transformers</span> <span class="kn">import</span> <span class="n">TransformerTokenClassifier</span>


<div class="viewcode-block" id="TrainTagger"><a class="viewcode-back" href="../../../generated_api/nlp_architect.procedures.html#nlp_architect.procedures.token_tagging.TrainTagger">[docs]</a><span class="nd">@register_train_cmd</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;tagger&quot;</span><span class="p">,</span> <span class="n">description</span><span class="o">=</span><span class="s2">&quot;Train a neural tagger&quot;</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">TrainTagger</span><span class="p">(</span><span class="n">Procedure</span><span class="p">):</span>
<div class="viewcode-block" id="TrainTagger.add_arguments"><a class="viewcode-back" href="../../../generated_api/nlp_architect.procedures.html#nlp_architect.procedures.token_tagging.TrainTagger.add_arguments">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">add_arguments</span><span class="p">(</span><span class="n">parser</span><span class="p">:</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">):</span>
        <span class="n">add_parse_args</span><span class="p">(</span><span class="n">parser</span><span class="p">)</span></div>

<div class="viewcode-block" id="TrainTagger.run_procedure"><a class="viewcode-back" href="../../../generated_api/nlp_architect.procedures.html#nlp_architect.procedures.token_tagging.TrainTagger.run_procedure">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">run_procedure</span><span class="p">(</span><span class="n">args</span><span class="p">):</span>
        <span class="n">do_training</span><span class="p">(</span><span class="n">args</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="TrainTaggerKD"><a class="viewcode-back" href="../../../generated_api/nlp_architect.procedures.html#nlp_architect.procedures.token_tagging.TrainTaggerKD">[docs]</a><span class="nd">@register_train_cmd</span><span class="p">(</span>
    <span class="n">name</span><span class="o">=</span><span class="s2">&quot;tagger_kd&quot;</span><span class="p">,</span>
    <span class="n">description</span><span class="o">=</span><span class="s2">&quot;Train a neural tagger using Knowledge Distillation&quot;</span>
    <span class="s2">&quot; and a Transformer teacher model&quot;</span><span class="p">,</span>
<span class="p">)</span>
<span class="k">class</span> <span class="nc">TrainTaggerKD</span><span class="p">(</span><span class="n">Procedure</span><span class="p">):</span>
<div class="viewcode-block" id="TrainTaggerKD.add_arguments"><a class="viewcode-back" href="../../../generated_api/nlp_architect.procedures.html#nlp_architect.procedures.token_tagging.TrainTaggerKD.add_arguments">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">add_arguments</span><span class="p">(</span><span class="n">parser</span><span class="p">:</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">):</span>
        <span class="n">add_parse_args</span><span class="p">(</span><span class="n">parser</span><span class="p">)</span>
        <span class="n">TeacherStudentDistill</span><span class="o">.</span><span class="n">add_args</span><span class="p">(</span><span class="n">parser</span><span class="p">)</span></div>

<div class="viewcode-block" id="TrainTaggerKD.run_procedure"><a class="viewcode-back" href="../../../generated_api/nlp_architect.procedures.html#nlp_architect.procedures.token_tagging.TrainTaggerKD.run_procedure">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">run_procedure</span><span class="p">(</span><span class="n">args</span><span class="p">):</span>
        <span class="n">do_kd_training</span><span class="p">(</span><span class="n">args</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="TrainTaggerKDPseudo"><a class="viewcode-back" href="../../../generated_api/nlp_architect.procedures.html#nlp_architect.procedures.token_tagging.TrainTaggerKDPseudo">[docs]</a><span class="nd">@register_train_cmd</span><span class="p">(</span>
    <span class="n">name</span><span class="o">=</span><span class="s2">&quot;tagger_kd_pseudo&quot;</span><span class="p">,</span>
    <span class="n">description</span><span class="o">=</span><span class="s2">&quot;Train a neural tagger using Knowledge Distillation&quot;</span>
    <span class="s2">&quot; and a Transformer teacher model + pseudo-labeling&quot;</span><span class="p">,</span>
<span class="p">)</span>
<span class="k">class</span> <span class="nc">TrainTaggerKDPseudo</span><span class="p">(</span><span class="n">Procedure</span><span class="p">):</span>
<div class="viewcode-block" id="TrainTaggerKDPseudo.add_arguments"><a class="viewcode-back" href="../../../generated_api/nlp_architect.procedures.html#nlp_architect.procedures.token_tagging.TrainTaggerKDPseudo.add_arguments">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">add_arguments</span><span class="p">(</span><span class="n">parser</span><span class="p">:</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">):</span>
        <span class="n">add_parse_args</span><span class="p">(</span><span class="n">parser</span><span class="p">)</span>
        <span class="n">TeacherStudentDistill</span><span class="o">.</span><span class="n">add_args</span><span class="p">(</span><span class="n">parser</span><span class="p">)</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
            <span class="s2">&quot;--labeled_train_file&quot;</span><span class="p">,</span>
            <span class="n">default</span><span class="o">=</span><span class="s2">&quot;labeled.txt&quot;</span><span class="p">,</span>
            <span class="nb">type</span><span class="o">=</span><span class="nb">str</span><span class="p">,</span>
            <span class="n">help</span><span class="o">=</span><span class="s2">&quot;The file name containing the labeled training examples&quot;</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
            <span class="s2">&quot;--unlabeled_train_file&quot;</span><span class="p">,</span>
            <span class="n">default</span><span class="o">=</span><span class="s2">&quot;unlabeled.txt&quot;</span><span class="p">,</span>
            <span class="nb">type</span><span class="o">=</span><span class="nb">str</span><span class="p">,</span>
            <span class="n">help</span><span class="o">=</span><span class="s2">&quot;The file name containing the unlabeled training examples&quot;</span><span class="p">,</span>
        <span class="p">)</span></div>

<div class="viewcode-block" id="TrainTaggerKDPseudo.run_procedure"><a class="viewcode-back" href="../../../generated_api/nlp_architect.procedures.html#nlp_architect.procedures.token_tagging.TrainTaggerKDPseudo.run_procedure">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">run_procedure</span><span class="p">(</span><span class="n">args</span><span class="p">):</span>
        <span class="n">do_kd_pseudo_training</span><span class="p">(</span><span class="n">args</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="RunTagger"><a class="viewcode-back" href="../../../generated_api/nlp_architect.procedures.html#nlp_architect.procedures.token_tagging.RunTagger">[docs]</a><span class="nd">@register_inference_cmd</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;tagger&quot;</span><span class="p">,</span> <span class="n">description</span><span class="o">=</span><span class="s2">&quot;Run a neural tagger model&quot;</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">RunTagger</span><span class="p">(</span><span class="n">Procedure</span><span class="p">):</span>
<div class="viewcode-block" id="RunTagger.add_arguments"><a class="viewcode-back" href="../../../generated_api/nlp_architect.procedures.html#nlp_architect.procedures.token_tagging.RunTagger.add_arguments">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">add_arguments</span><span class="p">(</span><span class="n">parser</span><span class="p">:</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">):</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
            <span class="s2">&quot;--data_file&quot;</span><span class="p">,</span>
            <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
            <span class="nb">type</span><span class="o">=</span><span class="nb">str</span><span class="p">,</span>
            <span class="n">required</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
            <span class="n">help</span><span class="o">=</span><span class="s2">&quot;The data file containing data for inference&quot;</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
            <span class="s2">&quot;--model_dir&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">str</span><span class="p">,</span> <span class="n">required</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Path to trained model directory&quot;</span>
        <span class="p">)</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
            <span class="s2">&quot;--output_dir&quot;</span><span class="p">,</span>
            <span class="nb">type</span><span class="o">=</span><span class="nb">str</span><span class="p">,</span>
            <span class="n">required</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
            <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Output directory where the model will be saved&quot;</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
            <span class="s2">&quot;--overwrite_output_dir&quot;</span><span class="p">,</span>
            <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_true&quot;</span><span class="p">,</span>
            <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Overwrite the content of the output directory&quot;</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
            <span class="s2">&quot;--no_cuda&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Avoid using CUDA when available&quot;</span>
        <span class="p">)</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;-b&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Batch size&quot;</span><span class="p">)</span></div>

<div class="viewcode-block" id="RunTagger.run_procedure"><a class="viewcode-back" href="../../../generated_api/nlp_architect.procedures.html#nlp_architect.procedures.token_tagging.RunTagger.run_procedure">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">run_procedure</span><span class="p">(</span><span class="n">args</span><span class="p">):</span>
        <span class="n">do_inference</span><span class="p">(</span><span class="n">args</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="add_parse_args"><a class="viewcode-back" href="../../../generated_api/nlp_architect.procedures.html#nlp_architect.procedures.token_tagging.add_parse_args">[docs]</a><span class="k">def</span> <span class="nf">add_parse_args</span><span class="p">(</span><span class="n">parser</span><span class="p">:</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">):</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
        <span class="s2">&quot;--model_type&quot;</span><span class="p">,</span>
        <span class="n">default</span><span class="o">=</span><span class="s2">&quot;cnn-lstm&quot;</span><span class="p">,</span>
        <span class="nb">type</span><span class="o">=</span><span class="nb">str</span><span class="p">,</span>
        <span class="n">choices</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="n">MODEL_TYPE</span><span class="o">.</span><span class="n">keys</span><span class="p">()),</span>
        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;model type to use for this tagger&quot;</span><span class="p">,</span>
    <span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--config_file&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">str</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Embedder model configuration file&quot;</span><span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;-b&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Batch size&quot;</span><span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;-b_ul&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Batch size of unlabeled data&quot;</span><span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;-e&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">155</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Number of epochs&quot;</span><span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
        <span class="s2">&quot;--data_dir&quot;</span><span class="p">,</span>
        <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="nb">type</span><span class="o">=</span><span class="nb">str</span><span class="p">,</span>
        <span class="n">required</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;The input data dir. Should contain dataset files to be parsed by &quot;</span> <span class="s2">&quot;the dataloaders.&quot;</span><span class="p">,</span>
    <span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
        <span class="s2">&quot;--tag_col&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">default</span><span class="o">=-</span><span class="mi">1</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Entity labels tab number in train/test files&quot;</span>
    <span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--max_sentence_length&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Max sentence length&quot;</span><span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
        <span class="s2">&quot;--max_word_length&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">12</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Max word length in characters&quot;</span>
    <span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
        <span class="s2">&quot;--use_crf&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Use CRF classifier instead of Softmax&quot;</span>
    <span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
        <span class="s2">&quot;--lr&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">float</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mf">0.001</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Learning rate for optimizer (Adam)&quot;</span>
    <span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--embedding_file&quot;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Path to external word embedding model file&quot;</span><span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
        <span class="s2">&quot;--output_dir&quot;</span><span class="p">,</span>
        <span class="nb">type</span><span class="o">=</span><span class="nb">str</span><span class="p">,</span>
        <span class="n">required</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Output directory where the model will be saved&quot;</span><span class="p">,</span>
    <span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--seed&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">42</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&quot;random seed for initialization&quot;</span><span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--logging_steps&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Log every X updates steps.&quot;</span><span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
        <span class="s2">&quot;--save_steps&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">500</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Save model every X updates steps.&quot;</span>
    <span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
        <span class="s2">&quot;--overwrite_output_dir&quot;</span><span class="p">,</span>
        <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_true&quot;</span><span class="p">,</span>
        <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Overwrite the content of the output directory&quot;</span><span class="p">,</span>
    <span class="p">)</span>
    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;--no_cuda&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Avoid using CUDA when available&quot;</span><span class="p">)</span></div>


<span class="n">MODEL_TYPE</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;cnn-lstm&quot;</span><span class="p">:</span> <span class="n">CNNLSTM</span><span class="p">,</span> <span class="s2">&quot;id-cnn&quot;</span><span class="p">:</span> <span class="n">IDCNN</span><span class="p">}</span>


<div class="viewcode-block" id="do_training"><a class="viewcode-back" href="../../../generated_api/nlp_architect.procedures.html#nlp_architect.procedures.token_tagging.do_training">[docs]</a><span class="k">def</span> <span class="nf">do_training</span><span class="p">(</span><span class="n">args</span><span class="p">):</span>
    <span class="n">prepare_output_path</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">output_dir</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">overwrite_output_dir</span><span class="p">)</span>
    <span class="n">device</span><span class="p">,</span> <span class="n">n_gpus</span> <span class="o">=</span> <span class="n">setup_backend</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">no_cuda</span><span class="p">)</span>
    <span class="c1"># Set seed</span>
    <span class="n">args</span><span class="o">.</span><span class="n">seed</span> <span class="o">=</span> <span class="n">set_seed</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">seed</span><span class="p">,</span> <span class="n">n_gpus</span><span class="p">)</span>
    <span class="c1"># prepare data</span>
    <span class="n">processor</span> <span class="o">=</span> <span class="n">TokenClsProcessor</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">data_dir</span><span class="p">,</span> <span class="n">tag_col</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">tag_col</span><span class="p">)</span>
    <span class="n">train_ex</span> <span class="o">=</span> <span class="n">processor</span><span class="o">.</span><span class="n">get_train_examples</span><span class="p">()</span>
    <span class="n">dev_ex</span> <span class="o">=</span> <span class="n">processor</span><span class="o">.</span><span class="n">get_dev_examples</span><span class="p">()</span>
    <span class="n">test_ex</span> <span class="o">=</span> <span class="n">processor</span><span class="o">.</span><span class="n">get_test_examples</span><span class="p">()</span>
    <span class="n">vocab</span> <span class="o">=</span> <span class="n">processor</span><span class="o">.</span><span class="n">get_vocabulary</span><span class="p">()</span>
    <span class="n">vocab_size</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">vocab</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
    <span class="n">num_labels</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">processor</span><span class="o">.</span><span class="n">get_labels</span><span class="p">())</span> <span class="o">+</span> <span class="mi">1</span>
    <span class="c1"># create an embedder</span>
    <span class="n">embedder_cls</span> <span class="o">=</span> <span class="n">MODEL_TYPE</span><span class="p">[</span><span class="n">args</span><span class="o">.</span><span class="n">model_type</span><span class="p">]</span>
    <span class="k">if</span> <span class="n">args</span><span class="o">.</span><span class="n">config_file</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">embedder_model</span> <span class="o">=</span> <span class="n">embedder_cls</span><span class="o">.</span><span class="n">from_config</span><span class="p">(</span><span class="n">vocab_size</span><span class="p">,</span> <span class="n">num_labels</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">config_file</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">embedder_model</span> <span class="o">=</span> <span class="n">embedder_cls</span><span class="p">(</span><span class="n">vocab_size</span><span class="p">,</span> <span class="n">num_labels</span><span class="p">)</span>

    <span class="c1"># load external word embeddings if present</span>
    <span class="k">if</span> <span class="n">args</span><span class="o">.</span><span class="n">embedding_file</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">emb_dict</span> <span class="o">=</span> <span class="n">load_embedding_file</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">embedding_file</span><span class="p">)</span>
        <span class="n">emb_mat</span> <span class="o">=</span> <span class="n">get_embedding_matrix</span><span class="p">(</span><span class="n">emb_dict</span><span class="p">,</span> <span class="n">vocab</span><span class="p">)</span>
        <span class="n">emb_mat</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">tensor</span><span class="p">(</span><span class="n">emb_mat</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">torch</span><span class="o">.</span><span class="n">float</span><span class="p">)</span>
        <span class="n">embedder_model</span><span class="o">.</span><span class="n">load_embeddings</span><span class="p">(</span><span class="n">emb_mat</span><span class="p">)</span>

    <span class="n">classifier</span> <span class="o">=</span> <span class="n">NeuralTagger</span><span class="p">(</span>
        <span class="n">embedder_model</span><span class="p">,</span>
        <span class="n">word_vocab</span><span class="o">=</span><span class="n">vocab</span><span class="p">,</span>
        <span class="n">labels</span><span class="o">=</span><span class="n">processor</span><span class="o">.</span><span class="n">get_labels</span><span class="p">(),</span>
        <span class="n">use_crf</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">use_crf</span><span class="p">,</span>
        <span class="n">device</span><span class="o">=</span><span class="n">device</span><span class="p">,</span>
        <span class="n">n_gpus</span><span class="o">=</span><span class="n">n_gpus</span><span class="p">,</span>
    <span class="p">)</span>

    <span class="n">train_batch_size</span> <span class="o">=</span> <span class="n">args</span><span class="o">.</span><span class="n">b</span> <span class="o">*</span> <span class="nb">max</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">n_gpus</span><span class="p">)</span>

    <span class="n">train_dataset</span> <span class="o">=</span> <span class="n">classifier</span><span class="o">.</span><span class="n">convert_to_tensors</span><span class="p">(</span>
        <span class="n">train_ex</span><span class="p">,</span> <span class="n">max_seq_length</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">max_sentence_length</span><span class="p">,</span> <span class="n">max_word_length</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">max_word_length</span>
    <span class="p">)</span>
    <span class="n">train_sampler</span> <span class="o">=</span> <span class="n">RandomSampler</span><span class="p">(</span><span class="n">train_dataset</span><span class="p">)</span>
    <span class="n">train_dl</span> <span class="o">=</span> <span class="n">DataLoader</span><span class="p">(</span><span class="n">train_dataset</span><span class="p">,</span> <span class="n">sampler</span><span class="o">=</span><span class="n">train_sampler</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="n">train_batch_size</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">dev_ex</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">dev_dataset</span> <span class="o">=</span> <span class="n">classifier</span><span class="o">.</span><span class="n">convert_to_tensors</span><span class="p">(</span>
            <span class="n">dev_ex</span><span class="p">,</span> <span class="n">max_seq_length</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">max_sentence_length</span><span class="p">,</span> <span class="n">max_word_length</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">max_word_length</span>
        <span class="p">)</span>
        <span class="n">dev_sampler</span> <span class="o">=</span> <span class="n">SequentialSampler</span><span class="p">(</span><span class="n">dev_dataset</span><span class="p">)</span>
        <span class="n">dev_dl</span> <span class="o">=</span> <span class="n">DataLoader</span><span class="p">(</span><span class="n">dev_dataset</span><span class="p">,</span> <span class="n">sampler</span><span class="o">=</span><span class="n">dev_sampler</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">b</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">test_ex</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">test_dataset</span> <span class="o">=</span> <span class="n">classifier</span><span class="o">.</span><span class="n">convert_to_tensors</span><span class="p">(</span>
            <span class="n">test_ex</span><span class="p">,</span> <span class="n">max_seq_length</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">max_sentence_length</span><span class="p">,</span> <span class="n">max_word_length</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">max_word_length</span>
        <span class="p">)</span>
        <span class="n">test_sampler</span> <span class="o">=</span> <span class="n">SequentialSampler</span><span class="p">(</span><span class="n">test_dataset</span><span class="p">)</span>
        <span class="n">test_dl</span> <span class="o">=</span> <span class="n">DataLoader</span><span class="p">(</span><span class="n">test_dataset</span><span class="p">,</span> <span class="n">sampler</span><span class="o">=</span><span class="n">test_sampler</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">b</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">args</span><span class="o">.</span><span class="n">lr</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">opt</span> <span class="o">=</span> <span class="n">classifier</span><span class="o">.</span><span class="n">get_optimizer</span><span class="p">(</span><span class="n">lr</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">lr</span><span class="p">)</span>
    <span class="n">classifier</span><span class="o">.</span><span class="n">train</span><span class="p">(</span>
        <span class="n">train_dl</span><span class="p">,</span>
        <span class="n">dev_dl</span><span class="p">,</span>
        <span class="n">test_dl</span><span class="p">,</span>
        <span class="n">epochs</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">e</span><span class="p">,</span>
        <span class="n">batch_size</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">b</span><span class="p">,</span>
        <span class="n">logging_steps</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">logging_steps</span><span class="p">,</span>
        <span class="n">save_steps</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">save_steps</span><span class="p">,</span>
        <span class="n">save_path</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">output_dir</span><span class="p">,</span>
        <span class="n">optimizer</span><span class="o">=</span><span class="n">opt</span> <span class="k">if</span> <span class="n">opt</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="kc">None</span><span class="p">,</span>
    <span class="p">)</span>
    <span class="n">classifier</span><span class="o">.</span><span class="n">save_model</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">output_dir</span><span class="p">)</span></div>


<div class="viewcode-block" id="do_kd_training"><a class="viewcode-back" href="../../../generated_api/nlp_architect.procedures.html#nlp_architect.procedures.token_tagging.do_kd_training">[docs]</a><span class="k">def</span> <span class="nf">do_kd_training</span><span class="p">(</span><span class="n">args</span><span class="p">):</span>
    <span class="n">prepare_output_path</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">output_dir</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">overwrite_output_dir</span><span class="p">)</span>
    <span class="n">device</span><span class="p">,</span> <span class="n">n_gpus</span> <span class="o">=</span> <span class="n">setup_backend</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">no_cuda</span><span class="p">)</span>
    <span class="c1"># Set seed</span>
    <span class="n">args</span><span class="o">.</span><span class="n">seed</span> <span class="o">=</span> <span class="n">set_seed</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">seed</span><span class="p">,</span> <span class="n">n_gpus</span><span class="p">)</span>
    <span class="c1"># prepare data</span>
    <span class="n">processor</span> <span class="o">=</span> <span class="n">TokenClsProcessor</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">data_dir</span><span class="p">,</span> <span class="n">tag_col</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">tag_col</span><span class="p">)</span>
    <span class="n">train_ex</span> <span class="o">=</span> <span class="n">processor</span><span class="o">.</span><span class="n">get_train_examples</span><span class="p">()</span>
    <span class="n">dev_ex</span> <span class="o">=</span> <span class="n">processor</span><span class="o">.</span><span class="n">get_dev_examples</span><span class="p">()</span>
    <span class="n">test_ex</span> <span class="o">=</span> <span class="n">processor</span><span class="o">.</span><span class="n">get_test_examples</span><span class="p">()</span>
    <span class="n">vocab</span> <span class="o">=</span> <span class="n">processor</span><span class="o">.</span><span class="n">get_vocabulary</span><span class="p">()</span>
    <span class="n">vocab_size</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">vocab</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
    <span class="n">num_labels</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">processor</span><span class="o">.</span><span class="n">get_labels</span><span class="p">())</span> <span class="o">+</span> <span class="mi">1</span>
    <span class="c1"># create an embedder</span>
    <span class="n">embedder_cls</span> <span class="o">=</span> <span class="n">MODEL_TYPE</span><span class="p">[</span><span class="n">args</span><span class="o">.</span><span class="n">model_type</span><span class="p">]</span>
    <span class="k">if</span> <span class="n">args</span><span class="o">.</span><span class="n">config_file</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">embedder_model</span> <span class="o">=</span> <span class="n">embedder_cls</span><span class="o">.</span><span class="n">from_config</span><span class="p">(</span><span class="n">vocab_size</span><span class="p">,</span> <span class="n">num_labels</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">config_file</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">embedder_model</span> <span class="o">=</span> <span class="n">embedder_cls</span><span class="p">(</span><span class="n">vocab_size</span><span class="p">,</span> <span class="n">num_labels</span><span class="p">)</span>

    <span class="c1"># load external word embeddings if present</span>
    <span class="k">if</span> <span class="n">args</span><span class="o">.</span><span class="n">embedding_file</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">emb_dict</span> <span class="o">=</span> <span class="n">load_embedding_file</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">embedding_file</span><span class="p">)</span>
        <span class="n">emb_mat</span> <span class="o">=</span> <span class="n">get_embedding_matrix</span><span class="p">(</span><span class="n">emb_dict</span><span class="p">,</span> <span class="n">vocab</span><span class="p">)</span>
        <span class="n">emb_mat</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">tensor</span><span class="p">(</span><span class="n">emb_mat</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">torch</span><span class="o">.</span><span class="n">float</span><span class="p">)</span>
        <span class="n">embedder_model</span><span class="o">.</span><span class="n">load_embeddings</span><span class="p">(</span><span class="n">emb_mat</span><span class="p">)</span>

    <span class="n">classifier</span> <span class="o">=</span> <span class="n">NeuralTagger</span><span class="p">(</span>
        <span class="n">embedder_model</span><span class="p">,</span>
        <span class="n">word_vocab</span><span class="o">=</span><span class="n">vocab</span><span class="p">,</span>
        <span class="n">labels</span><span class="o">=</span><span class="n">processor</span><span class="o">.</span><span class="n">get_labels</span><span class="p">(),</span>
        <span class="n">use_crf</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">use_crf</span><span class="p">,</span>
        <span class="n">device</span><span class="o">=</span><span class="n">device</span><span class="p">,</span>
        <span class="n">n_gpus</span><span class="o">=</span><span class="n">n_gpus</span><span class="p">,</span>
    <span class="p">)</span>

    <span class="n">train_batch_size</span> <span class="o">=</span> <span class="n">args</span><span class="o">.</span><span class="n">b</span> <span class="o">*</span> <span class="nb">max</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">n_gpus</span><span class="p">)</span>
    <span class="n">train_dataset</span> <span class="o">=</span> <span class="n">classifier</span><span class="o">.</span><span class="n">convert_to_tensors</span><span class="p">(</span>
        <span class="n">train_ex</span><span class="p">,</span> <span class="n">max_seq_length</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">max_sentence_length</span><span class="p">,</span> <span class="n">max_word_length</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">max_word_length</span>
    <span class="p">)</span>

    <span class="n">teacher</span> <span class="o">=</span> <span class="n">TransformerTokenClassifier</span><span class="o">.</span><span class="n">load_model</span><span class="p">(</span>
        <span class="n">model_path</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">teacher_model_path</span><span class="p">,</span> <span class="n">model_type</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">teacher_model_type</span>
    <span class="p">)</span>
    <span class="n">teacher</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">device</span><span class="p">,</span> <span class="n">n_gpus</span><span class="p">)</span>
    <span class="n">teacher_dataset</span> <span class="o">=</span> <span class="n">teacher</span><span class="o">.</span><span class="n">convert_to_tensors</span><span class="p">(</span><span class="n">train_ex</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">max_sentence_length</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>

    <span class="n">train_dataset</span> <span class="o">=</span> <span class="n">ParallelDataset</span><span class="p">(</span><span class="n">train_dataset</span><span class="p">,</span> <span class="n">teacher_dataset</span><span class="p">)</span>

    <span class="n">train_sampler</span> <span class="o">=</span> <span class="n">RandomSampler</span><span class="p">(</span><span class="n">train_dataset</span><span class="p">)</span>
    <span class="n">train_dl</span> <span class="o">=</span> <span class="n">DataLoader</span><span class="p">(</span><span class="n">train_dataset</span><span class="p">,</span> <span class="n">sampler</span><span class="o">=</span><span class="n">train_sampler</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="n">train_batch_size</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">dev_ex</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">dev_dataset</span> <span class="o">=</span> <span class="n">classifier</span><span class="o">.</span><span class="n">convert_to_tensors</span><span class="p">(</span>
            <span class="n">dev_ex</span><span class="p">,</span> <span class="n">max_seq_length</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">max_sentence_length</span><span class="p">,</span> <span class="n">max_word_length</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">max_word_length</span>
        <span class="p">)</span>
        <span class="n">dev_sampler</span> <span class="o">=</span> <span class="n">SequentialSampler</span><span class="p">(</span><span class="n">dev_dataset</span><span class="p">)</span>
        <span class="n">dev_dl</span> <span class="o">=</span> <span class="n">DataLoader</span><span class="p">(</span><span class="n">dev_dataset</span><span class="p">,</span> <span class="n">sampler</span><span class="o">=</span><span class="n">dev_sampler</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">b</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">test_ex</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">test_dataset</span> <span class="o">=</span> <span class="n">classifier</span><span class="o">.</span><span class="n">convert_to_tensors</span><span class="p">(</span>
            <span class="n">test_ex</span><span class="p">,</span> <span class="n">max_seq_length</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">max_sentence_length</span><span class="p">,</span> <span class="n">max_word_length</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">max_word_length</span>
        <span class="p">)</span>
        <span class="n">test_sampler</span> <span class="o">=</span> <span class="n">SequentialSampler</span><span class="p">(</span><span class="n">test_dataset</span><span class="p">)</span>
        <span class="n">test_dl</span> <span class="o">=</span> <span class="n">DataLoader</span><span class="p">(</span><span class="n">test_dataset</span><span class="p">,</span> <span class="n">sampler</span><span class="o">=</span><span class="n">test_sampler</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">b</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">args</span><span class="o">.</span><span class="n">lr</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">opt</span> <span class="o">=</span> <span class="n">classifier</span><span class="o">.</span><span class="n">get_optimizer</span><span class="p">(</span><span class="n">lr</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">lr</span><span class="p">)</span>

    <span class="n">distiller</span> <span class="o">=</span> <span class="n">TeacherStudentDistill</span><span class="p">(</span>
        <span class="n">teacher</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">kd_temp</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">kd_dist_w</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">kd_student_w</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">kd_loss_fn</span>
    <span class="p">)</span>
    <span class="n">classifier</span><span class="o">.</span><span class="n">train</span><span class="p">(</span>
        <span class="n">train_dl</span><span class="p">,</span>
        <span class="n">dev_dl</span><span class="p">,</span>
        <span class="n">test_dl</span><span class="p">,</span>
        <span class="n">epochs</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">e</span><span class="p">,</span>
        <span class="n">batch_size</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">b</span><span class="p">,</span>
        <span class="n">logging_steps</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">logging_steps</span><span class="p">,</span>
        <span class="n">save_steps</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">save_steps</span><span class="p">,</span>
        <span class="n">save_path</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">output_dir</span><span class="p">,</span>
        <span class="n">optimizer</span><span class="o">=</span><span class="n">opt</span> <span class="k">if</span> <span class="n">opt</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">distiller</span><span class="o">=</span><span class="n">distiller</span><span class="p">,</span>
    <span class="p">)</span>
    <span class="n">classifier</span><span class="o">.</span><span class="n">save_model</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">output_dir</span><span class="p">)</span></div>


<div class="viewcode-block" id="do_kd_pseudo_training"><a class="viewcode-back" href="../../../generated_api/nlp_architect.procedures.html#nlp_architect.procedures.token_tagging.do_kd_pseudo_training">[docs]</a><span class="k">def</span> <span class="nf">do_kd_pseudo_training</span><span class="p">(</span><span class="n">args</span><span class="p">):</span>
    <span class="n">prepare_output_path</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">output_dir</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">overwrite_output_dir</span><span class="p">)</span>
    <span class="n">device</span><span class="p">,</span> <span class="n">n_gpus</span> <span class="o">=</span> <span class="n">setup_backend</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">no_cuda</span><span class="p">)</span>
    <span class="c1"># Set seed</span>
    <span class="n">set_seed</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">seed</span><span class="p">,</span> <span class="n">n_gpus</span><span class="p">)</span>
    <span class="c1"># prepare data</span>
    <span class="n">processor</span> <span class="o">=</span> <span class="n">TokenClsProcessor</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">data_dir</span><span class="p">,</span> <span class="n">tag_col</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">tag_col</span><span class="p">)</span>
    <span class="n">train_labeled_ex</span> <span class="o">=</span> <span class="n">processor</span><span class="o">.</span><span class="n">get_train_examples</span><span class="p">(</span><span class="n">filename</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">labeled_train_file</span><span class="p">)</span>
    <span class="n">train_unlabeled_ex</span> <span class="o">=</span> <span class="n">processor</span><span class="o">.</span><span class="n">get_train_examples</span><span class="p">(</span><span class="n">filename</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">unlabeled_train_file</span><span class="p">)</span>
    <span class="n">dev_ex</span> <span class="o">=</span> <span class="n">processor</span><span class="o">.</span><span class="n">get_dev_examples</span><span class="p">()</span>
    <span class="n">test_ex</span> <span class="o">=</span> <span class="n">processor</span><span class="o">.</span><span class="n">get_test_examples</span><span class="p">()</span>
    <span class="n">vocab</span> <span class="o">=</span> <span class="n">processor</span><span class="o">.</span><span class="n">get_vocabulary</span><span class="p">()</span>
    <span class="n">vocab_size</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">vocab</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
    <span class="n">num_labels</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">processor</span><span class="o">.</span><span class="n">get_labels</span><span class="p">())</span> <span class="o">+</span> <span class="mi">1</span>
    <span class="c1"># create an embedder</span>
    <span class="n">embedder_cls</span> <span class="o">=</span> <span class="n">MODEL_TYPE</span><span class="p">[</span><span class="n">args</span><span class="o">.</span><span class="n">model_type</span><span class="p">]</span>
    <span class="k">if</span> <span class="n">args</span><span class="o">.</span><span class="n">config_file</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">embedder_model</span> <span class="o">=</span> <span class="n">embedder_cls</span><span class="o">.</span><span class="n">from_config</span><span class="p">(</span><span class="n">vocab_size</span><span class="p">,</span> <span class="n">num_labels</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">config_file</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">embedder_model</span> <span class="o">=</span> <span class="n">embedder_cls</span><span class="p">(</span><span class="n">vocab_size</span><span class="p">,</span> <span class="n">num_labels</span><span class="p">)</span>

    <span class="c1"># load external word embeddings if present</span>
    <span class="k">if</span> <span class="n">args</span><span class="o">.</span><span class="n">embedding_file</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">emb_dict</span> <span class="o">=</span> <span class="n">load_embedding_file</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">embedding_file</span><span class="p">)</span>
        <span class="n">emb_mat</span> <span class="o">=</span> <span class="n">get_embedding_matrix</span><span class="p">(</span><span class="n">emb_dict</span><span class="p">,</span> <span class="n">vocab</span><span class="p">)</span>
        <span class="n">emb_mat</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">tensor</span><span class="p">(</span><span class="n">emb_mat</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">torch</span><span class="o">.</span><span class="n">float</span><span class="p">)</span>
        <span class="n">embedder_model</span><span class="o">.</span><span class="n">load_embeddings</span><span class="p">(</span><span class="n">emb_mat</span><span class="p">)</span>

    <span class="n">classifier</span> <span class="o">=</span> <span class="n">NeuralTagger</span><span class="p">(</span>
        <span class="n">embedder_model</span><span class="p">,</span>
        <span class="n">word_vocab</span><span class="o">=</span><span class="n">vocab</span><span class="p">,</span>
        <span class="n">labels</span><span class="o">=</span><span class="n">processor</span><span class="o">.</span><span class="n">get_labels</span><span class="p">(),</span>
        <span class="n">use_crf</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">use_crf</span><span class="p">,</span>
        <span class="n">device</span><span class="o">=</span><span class="n">device</span><span class="p">,</span>
        <span class="n">n_gpus</span><span class="o">=</span><span class="n">n_gpus</span><span class="p">,</span>
    <span class="p">)</span>

    <span class="n">train_batch_size</span> <span class="o">=</span> <span class="n">args</span><span class="o">.</span><span class="n">b</span> <span class="o">*</span> <span class="nb">max</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">n_gpus</span><span class="p">)</span>
    <span class="n">train_batch_size_ul</span> <span class="o">=</span> <span class="n">args</span><span class="o">.</span><span class="n">b_ul</span> <span class="o">*</span> <span class="nb">max</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">n_gpus</span><span class="p">)</span>
    <span class="n">train_labeled_dataset</span> <span class="o">=</span> <span class="n">classifier</span><span class="o">.</span><span class="n">convert_to_tensors</span><span class="p">(</span>
        <span class="n">train_labeled_ex</span><span class="p">,</span>
        <span class="n">max_seq_length</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">max_sentence_length</span><span class="p">,</span>
        <span class="n">max_word_length</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">max_word_length</span><span class="p">,</span>
    <span class="p">)</span>
    <span class="n">train_unlabeled_dataset</span> <span class="o">=</span> <span class="n">classifier</span><span class="o">.</span><span class="n">convert_to_tensors</span><span class="p">(</span>
        <span class="n">train_unlabeled_ex</span><span class="p">,</span>
        <span class="n">max_seq_length</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">max_sentence_length</span><span class="p">,</span>
        <span class="n">max_word_length</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">max_word_length</span><span class="p">,</span>
        <span class="n">include_labels</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="p">)</span>
    <span class="n">teacher</span> <span class="o">=</span> <span class="n">TransformerTokenClassifier</span><span class="o">.</span><span class="n">load_model</span><span class="p">(</span>
        <span class="n">model_path</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">teacher_model_path</span><span class="p">,</span> <span class="n">model_type</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">teacher_model_type</span>
    <span class="p">)</span>
    <span class="n">teacher</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">device</span><span class="p">,</span> <span class="n">n_gpus</span><span class="p">)</span>
    <span class="n">teacher_labeled_dataset</span> <span class="o">=</span> <span class="n">teacher</span><span class="o">.</span><span class="n">convert_to_tensors</span><span class="p">(</span>
        <span class="n">train_labeled_ex</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">max_sentence_length</span><span class="p">,</span> <span class="kc">False</span>
    <span class="p">)</span>
    <span class="n">teacher_unlabeled_dataset</span> <span class="o">=</span> <span class="n">teacher</span><span class="o">.</span><span class="n">convert_to_tensors</span><span class="p">(</span>
        <span class="n">train_unlabeled_ex</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">max_sentence_length</span><span class="p">,</span> <span class="kc">False</span>
    <span class="p">)</span>
    <span class="n">train_labeled_dataset</span> <span class="o">=</span> <span class="n">ParallelDataset</span><span class="p">(</span><span class="n">train_labeled_dataset</span><span class="p">,</span> <span class="n">teacher_labeled_dataset</span><span class="p">)</span>
    <span class="n">train_unlabeled_dataset</span> <span class="o">=</span> <span class="n">ParallelDataset</span><span class="p">(</span><span class="n">train_unlabeled_dataset</span><span class="p">,</span> <span class="n">teacher_unlabeled_dataset</span><span class="p">)</span>
    <span class="n">train_labeled_sampler</span> <span class="o">=</span> <span class="n">RandomSampler</span><span class="p">(</span><span class="n">train_labeled_dataset</span><span class="p">)</span>
    <span class="n">train_unlabeled_sampler</span> <span class="o">=</span> <span class="n">RandomSampler</span><span class="p">(</span><span class="n">train_unlabeled_dataset</span><span class="p">)</span>
    <span class="n">train_labeled_dl</span> <span class="o">=</span> <span class="n">DataLoader</span><span class="p">(</span>
        <span class="n">train_labeled_dataset</span><span class="p">,</span> <span class="n">sampler</span><span class="o">=</span><span class="n">train_labeled_sampler</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="n">train_batch_size</span>
    <span class="p">)</span>
    <span class="n">train_unlabeled_dl</span> <span class="o">=</span> <span class="n">DataLoader</span><span class="p">(</span>
        <span class="n">train_unlabeled_dataset</span><span class="p">,</span> <span class="n">sampler</span><span class="o">=</span><span class="n">train_unlabeled_sampler</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="n">train_batch_size_ul</span>
    <span class="p">)</span>

    <span class="k">if</span> <span class="n">dev_ex</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">dev_dataset</span> <span class="o">=</span> <span class="n">classifier</span><span class="o">.</span><span class="n">convert_to_tensors</span><span class="p">(</span>
            <span class="n">dev_ex</span><span class="p">,</span> <span class="n">max_seq_length</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">max_sentence_length</span><span class="p">,</span> <span class="n">max_word_length</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">max_word_length</span>
        <span class="p">)</span>
        <span class="n">dev_sampler</span> <span class="o">=</span> <span class="n">SequentialSampler</span><span class="p">(</span><span class="n">dev_dataset</span><span class="p">)</span>
        <span class="n">dev_dl</span> <span class="o">=</span> <span class="n">DataLoader</span><span class="p">(</span><span class="n">dev_dataset</span><span class="p">,</span> <span class="n">sampler</span><span class="o">=</span><span class="n">dev_sampler</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">b</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">test_ex</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">test_dataset</span> <span class="o">=</span> <span class="n">classifier</span><span class="o">.</span><span class="n">convert_to_tensors</span><span class="p">(</span>
            <span class="n">test_ex</span><span class="p">,</span> <span class="n">max_seq_length</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">max_sentence_length</span><span class="p">,</span> <span class="n">max_word_length</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">max_word_length</span>
        <span class="p">)</span>
        <span class="n">test_sampler</span> <span class="o">=</span> <span class="n">SequentialSampler</span><span class="p">(</span><span class="n">test_dataset</span><span class="p">)</span>
        <span class="n">test_dl</span> <span class="o">=</span> <span class="n">DataLoader</span><span class="p">(</span><span class="n">test_dataset</span><span class="p">,</span> <span class="n">sampler</span><span class="o">=</span><span class="n">test_sampler</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">b</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">args</span><span class="o">.</span><span class="n">lr</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">opt</span> <span class="o">=</span> <span class="n">classifier</span><span class="o">.</span><span class="n">get_optimizer</span><span class="p">(</span><span class="n">lr</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">lr</span><span class="p">)</span>

    <span class="n">distiller</span> <span class="o">=</span> <span class="n">TeacherStudentDistill</span><span class="p">(</span>
        <span class="n">teacher</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">kd_temp</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">kd_dist_w</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">kd_student_w</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">kd_loss_fn</span>
    <span class="p">)</span>
    <span class="n">classifier</span><span class="o">.</span><span class="n">train_pseudo</span><span class="p">(</span>
        <span class="n">train_labeled_dl</span><span class="p">,</span>
        <span class="n">train_unlabeled_dl</span><span class="p">,</span>
        <span class="n">distiller</span><span class="p">,</span>
        <span class="n">dev_dl</span><span class="p">,</span>
        <span class="n">test_dl</span><span class="p">,</span>
        <span class="n">batch_size_l</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">b</span><span class="p">,</span>
        <span class="n">batch_size_ul</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">b_ul</span><span class="p">,</span>
        <span class="n">epochs</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">e</span><span class="p">,</span>
        <span class="n">logging_steps</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">logging_steps</span><span class="p">,</span>
        <span class="n">save_steps</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">save_steps</span><span class="p">,</span>
        <span class="n">save_path</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">output_dir</span><span class="p">,</span>
        <span class="n">optimizer</span><span class="o">=</span><span class="n">opt</span> <span class="k">if</span> <span class="n">opt</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="kc">None</span><span class="p">,</span>
    <span class="p">)</span>
    <span class="n">classifier</span><span class="o">.</span><span class="n">save_model</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">output_dir</span><span class="p">)</span></div>


<div class="viewcode-block" id="do_inference"><a class="viewcode-back" href="../../../generated_api/nlp_architect.procedures.html#nlp_architect.procedures.token_tagging.do_inference">[docs]</a><span class="k">def</span> <span class="nf">do_inference</span><span class="p">(</span><span class="n">args</span><span class="p">):</span>
    <span class="n">prepare_output_path</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">output_dir</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">overwrite_output_dir</span><span class="p">)</span>
    <span class="n">device</span><span class="p">,</span> <span class="n">n_gpus</span> <span class="o">=</span> <span class="n">setup_backend</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">no_cuda</span><span class="p">)</span>
    <span class="n">args</span><span class="o">.</span><span class="n">batch_size</span> <span class="o">=</span> <span class="n">args</span><span class="o">.</span><span class="n">b</span> <span class="o">*</span> <span class="nb">max</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">n_gpus</span><span class="p">)</span>
    <span class="n">inference_examples</span> <span class="o">=</span> <span class="n">process_inference_input</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">data_file</span><span class="p">)</span>
    <span class="n">classifier</span> <span class="o">=</span> <span class="n">NeuralTagger</span><span class="o">.</span><span class="n">load_model</span><span class="p">(</span><span class="n">model_path</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">model_dir</span><span class="p">)</span>
    <span class="n">classifier</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">device</span><span class="p">,</span> <span class="n">n_gpus</span><span class="p">)</span>
    <span class="n">output</span> <span class="o">=</span> <span class="n">classifier</span><span class="o">.</span><span class="n">inference</span><span class="p">(</span><span class="n">inference_examples</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">b</span><span class="p">)</span>
    <span class="n">write_column_tagged_file</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">output_dir</span> <span class="o">+</span> <span class="n">os</span><span class="o">.</span><span class="n">sep</span> <span class="o">+</span> <span class="s2">&quot;output.txt&quot;</span><span class="p">,</span> <span class="n">output</span><span class="p">)</span></div>


<div class="viewcode-block" id="process_inference_input"><a class="viewcode-back" href="../../../generated_api/nlp_architect.procedures.html#nlp_architect.procedures.token_tagging.process_inference_input">[docs]</a><span class="k">def</span> <span class="nf">process_inference_input</span><span class="p">(</span><span class="n">input_file</span><span class="p">):</span>
    <span class="k">with</span> <span class="n">io</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">input_file</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>
        <span class="n">texts</span> <span class="o">=</span> <span class="p">[</span><span class="n">l</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="k">for</span> <span class="n">l</span> <span class="ow">in</span> <span class="n">fp</span><span class="o">.</span><span class="n">readlines</span><span class="p">()]</span>
    <span class="n">tokenizer</span> <span class="o">=</span> <span class="n">SpacyInstance</span><span class="p">(</span><span class="n">disable</span><span class="o">=</span><span class="p">[</span><span class="s2">&quot;tagger&quot;</span><span class="p">,</span> <span class="s2">&quot;parser&quot;</span><span class="p">,</span> <span class="s2">&quot;ner&quot;</span><span class="p">])</span>
    <span class="n">examples</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">t</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">texts</span><span class="p">):</span>
        <span class="n">examples</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">TokenClsInputExample</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">),</span> <span class="n">t</span><span class="p">,</span> <span class="n">tokenizer</span><span class="o">.</span><span class="n">tokenize</span><span class="p">(</span><span class="n">t</span><span class="p">)))</span>
    <span class="k">return</span> <span class="n">examples</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>